<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Student Login - Teaching Management System</title>
  <link rel="stylesheet" href="account-login.css" />
</head>
<body>
  <div class="container">
    <h2>Welcome to AceHub</h2>
    <form id="login-form">
      <div class="input-group">
        <label for="userid">Student ID</label>
        <input type="text" id="userid" placeholder="Please enter your student ID" required />
        <span id="us-error-message" class="error-message"></span>
      </div>

      <div class="input-group">
        <label for="password">Password <a href="forgot-password.html">Forgot password?</a></label>
        <input type="password" id="password" placeholder="Please enter your password" required />
        <span id="pa-error-message" class="error-message"></span>
      </div>

      <div class="checkbox-group">
        <div class="checkbox-item">
          <input type="checkbox" id="remember-me" />
          <label for="remember-me">Remember the account</label>
        </div>
        <div class="checkbox-item">
          <input type="checkbox" id="agree-policy" required />
          <label for="agree-policy">I have agreed <a href="../Homepage/privacy.html" target="_blank">Privacy Policy</a></label>
        </div>
      </div>

      <button id="btn" type="submit" class="button">
        <span class="button-text">Log in</span>
      </button>

      <div id="success-message" class="success-message"></div>
    </form>

    <div class="auth-links">
      <p>
        Don't have an account?<a href="register.html">Register Now</a><br />
        
      </p>
    </div>
  </div>

  <!-- 登录中加载覆盖层 -->
<!-- 遮罩层 -->
    <div id="loading-overlay">
    <div class="loading-box">
        <div class="spinner"></div>
        <p>Logging in</p>
    </div>
    </div>


  <script src="../database.js"></script>
  <script src="account.js"></script>
  <script>
    const overlay = document.getElementById("loading-overlay");

    function showLoading() {
    document.getElementById("loading-overlay").classList.add("show");
    }
    function hideLoading() {
    document.getElementById("loading-overlay").classList.remove("show");
    }


    window.onload = function () {
      const rememberMe = localStorage.getItem("rememberMe");
      if (rememberMe) {
        document.getElementById("userid").value = rememberMe;
        document.getElementById("remember-me").checked = true;
      }
      localStorage.removeItem("token");
      hideLoading(); // Ensure the loading overlay is hidden when entering the page
    };

    document.getElementById("userid").addEventListener("blur", checkUserid);
    document.getElementById("password").addEventListener("blur", checkPassword);

    let errorCount = 0;
    const maxErrors = 5;
    let freezeTime = 5000;

    document.getElementById("login-form").addEventListener("submit", (e) => {
      e.preventDefault();

      const button = document.getElementById("btn");
      const userid = document.getElementById("userid").value;
      const password = document.getElementById("password").value;
      const rememberMe = document.getElementById("remember-me").checked;
      const agreePolicy = document.getElementById("agree-policy").checked;

      if (!agreePolicy) {
        alert("Please agree to the privacy policy first");
        return;
      }

      if (!checkUserid() || !checkPassword()) {
        return;
      }

      showLoading();

      setTimeout(() => {
        const item = JSON.parse(localStorage.getItem(userid));
        if (!item) {
          document.getElementById("us-error-message").textContent = "Student ID does not exist";
          hideLoading();
          return;
        }

        if (!isCorrect(password, userid, item.password)) {
          document.getElementById("pa-error-message").textContent = `Wrong password, account will be frozen for ${freezeTime / 1000} seconds after ${maxErrors} failed attempts`;
          document.getElementById("password").value = "";
          errorCount++;

          if (errorCount >= maxErrors) {
            alert("Account has been frozen, please try again later");
            button.disabled = true;
            document.getElementById("login-form").style.pointerEvents = "none";
            setTimeout(() => {
              button.disabled = false;
              document.getElementById("login-form").style.pointerEvents = "";
              errorCount = 0;
            }, freezeTime);
            freezeTime *= 2;
          }
          hideLoading();
          return;
        }

        if (!item.status) {
          alert("Account has been disabled, please contact the teacher");
          hideLoading();
          return;
        }

        // document.getElementById("success-message").textContent = "Login successful！Redirecting...";
        localStorage.setItem("token", userid);
        if (rememberMe) {
          localStorage.setItem("rememberMe", userid);
        } else {
          localStorage.removeItem("rememberMe");
        }

        errorCount = 0;
        freezeTime = 5000;

        setTimeout(() => {
          hideLoading();
          if (item.identity === "student") {
            window.location.href = "../student/student-profile.html";
          } else if (item.identity === "teacher") {
            window.location.href = "../teacher/teacher-profile.html";
          } else if (item.identity === "admin") {
            window.location.href = "../account/admin-profile.html";
          }
        }, 1500);
      }, 800);
    });

    function checkUserid() {
      const userid = document.getElementById("userid").value;
      if (!userid) {
        document.getElementById("us-error-message").textContent = "Student ID cannot be empty";
        return false;
      }
      document.getElementById("us-error-message").textContent = "";
      return true;
    }

    function checkPassword() {
      const password = document.getElementById("password").value;
      if (!password) {
        document.getElementById("pa-error-message").textContent = "Password cannot be empty";
        return false;
      }
      document.getElementById("pa-error-message").textContent = "";
      return true;
    }

    function isCorrect(password, userid, tPassword) {
      // Here we use your previous simpleHash function for password verification, make sure simpleHash is defined
      password = simpleHash(password, userid);
      return password === tPassword;
    }

    document.querySelectorAll(".input-group input").forEach((input) => {
      input.addEventListener("focus", function () {
        this.parentElement.classList.add("focused");
      });
      input.addEventListener("blur", function () {
        if (!this.value) {
          this.parentElement.classList.remove("focused");
        }
      });
    });
  </script>
</body>
</html>